#include <func.h>

typedef struct
{
    int file_len;
    char file_name[50];
} File;

int main(void)
{
    File file;
    strcpy(file.file_name, "a.mp4");
    file.file_len = strlen(file.file_name);
    printf("sizeof(file) = %ld\n", sizeof(file));

    int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi("8080"));
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(connect(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
        printf("未找到服务器\n");
        error(1, errno, "connect");
    }

    printf("已连接服务器\n");

    send(sock_fd, &file, sizeof(file), 0);

    int file_fd = open(file.file_name, O_RDONLY);
    if(file_fd == -1)
        error(1, errno, "open");
    printf("文件名为：%s\n", file.file_name);

    while(1)
    {        
        char buf[128] = {0};
        int count = read(file_fd, buf, sizeof(buf));
        if(count == 0)
        {
            printf("传输完成\n");
            break;
        }
        send(sock_fd, buf, count, 0);
    }

    system("sha1sum a.mp4");
    close(file_fd);
    close(sock_fd);
    return 0;
}

